Description:
USP detects
non-sealed
classes which have only
static
members.
Such classes cannot be customized, i.e., it is not possible to derive
a subclass from it and redefine one or more methods; therefore, the Singleton
pattern should be used instead. According to this pattern,
all static
variables and methods should be replaced with
instance members and there should be a static
method
GetInstance()
that returns the single instance of the class.
Singletons should declare
private no-argument constructors
to show that they are not intended to be instantiated.
Incorrect:
public class Registry {
private static Hashtable map = new Hashtable();
public static String GetProperty(string name) {
...
}
}
Correct:
public class Registry {
private static Registry instance = new Registry();
private Hashtable map = new Hashtable();
public static Registry GetInstance() {
return instance;
}
public String GetProperty(string name) {
...
}
}